#!/usr/bin/env python

# Copyright (c) 2012 by Albert Graef <Dr.Graef@t-online.de>. All rights
# reserved. faust-lv2 is distributed under the GNU Lesser General Public
# License version 3 or later. Please see the included COPYING and
# COPYING.LESSER files for details.

# QUICK SUMMARY: './waf configure --help' shows the available build options.
# Run './waf configure' and './waf' to build the software, and (optionally)
# 'sudo ./waf install' to install. Please check the README file for further
# information.

# Package name and version.
APPNAME = 'faust-lv2'
VERSION = '0.4'

# Set this to the default URI prefix for your plugins. This can be any URI
# uniquely identifying the origin of your plugins. Note that this option can
# also be overridden with the --uri-prefix configure option.
URI_PREFIX = 'http://faust-lv2.googlecode.com'

# No need to edit below this line (usually). ###############################

top = '.'
out = 'build'

subdirs = ['effects', 'synths']

def options(opt):
    opt.load('compiler_cxx boost')

    opt.add_option('--debug', action='store_true', default=False,
                   help='build debuggable binaries (-g)')
    opt.add_option('--debug-meta', action='store_true', default=False,
                   help='debug Faust metadata')
    opt.add_option('--debug-voices', action='store_true', default=False,
                   help='debug voice triggering (synth only)')
    opt.add_option('--debug-voice-alloc', action='store_true', default=False,
                   help='debug voice allocation (synth only)')
    opt.add_option('--debug-midi', action='store_true', default=False,
                   help='debug midi input')
    opt.add_option('--debug-notes', action='store_true', default=False,
                   help='debug midi notes')
    opt.add_option('--debug-midicc', action='store_true', default=False,
                   help='debug midi controllers')
    opt.add_option('--debug-mts', action='store_true', default=False,
                   help='debug midi tunings')
    opt.add_option('--debug-rpn', action='store_true', default=False,
                   help='debug rpn messages')

    opt.add_option('--dyn-manifest', action='store_true', default=False,
                   help='use the dynamic manifest extension')

    opt.add_option('--faustdir', action='store',
		   default='${LIBDIR}/faust',
		   help='installation dir for Faust architecture files [default: LIBDIR/faust]')
    opt.add_option('--lv2dir', action='store',
		   default='${LIBDIR}/lv2',
		   help='installation dir for LV2 plugins [default: LIBDIR/lv2]')
    opt.add_option('--plugindir', action='store',
		   default='faust.lv2',
		   help='name of plugin dir [default: faust.lv2]')
    opt.add_option('--split-bundles', action='store_true', default=False,
                   help='build separate bundles [default: one bundle]')

    opt.add_option('--no-faust-install', action='store_true', default=False,
                   help='do not install the Faust architecture files')
    opt.add_option('--no-plugin-install', action='store_true', default=False,
                   help='do not install the sample plugins')

    opt.add_option('--nometa', action='store_true', default=False,
                   help='ignore Faust metadata (author, name, etc.)')
    opt.add_option('--nomidicc', action='store_true', default=False,
                   help='ignore Faust midi controller mapping (midi:ctrl)')
    opt.add_option('--nvoices', action='store', default=16,
		   help='number of synth voices (polyphony) [default: 16]')
    opt.add_option('--uri-prefix', action='store',
		   default=URI_PREFIX,
		   help='URI prefix for LV2 plugins [default: %s]' % URI_PREFIX)

def configure(conf):
    conf.load('compiler_cxx boost')
    # we only need this header from boost
    conf.check(header_name="boost/circular_buffer.hpp")
    if conf.options.debug:
        conf.env.CXXFLAGS = ['-O2', '-g']
    else:
        conf.env.CXXFLAGS = ['-O3', '-ffast-math', '-msse']
    conf.define('PACKAGE_VERSION', VERSION)
    conf.define('URI_PREFIX', conf.options.uri_prefix)
    try:
        conf.define('NVOICES', int(conf.options.nvoices))
    except ValueError:
        conf.define('NVOICES', 16)
        print "Warning: invalid --nvoices value (ignored)"
    conf.define('FAUST_META', int(not conf.options.nometa))
    conf.define('FAUST_MIDICC', int(not conf.options.nomidicc))
    #conf.write_config_header('config.h')
    conf.env.mydefines = [ "%s=%s" % (key, conf.get_define(key)) for key in
                           ['PACKAGE_VERSION', 'URI_PREFIX', 'NVOICES',
                            'FAUST_META', 'FAUST_MIDICC'] ]
    if conf.options.debug_meta:
        conf.env.mydefines.append('DEBUG_META')
    if conf.options.debug_voices:
        conf.env.mydefines.append('DEBUG_VOICES')
    if conf.options.debug_voice_alloc:
        conf.env.mydefines.append('DEBUG_VOICE_ALLOC')
    if conf.options.debug_midi:
        conf.env.mydefines.append('DEBUG_MIDI')
    if conf.options.debug_notes:
        conf.env.mydefines.append('DEBUG_NOTES')
    if conf.options.debug_midicc:
        conf.env.mydefines.append('DEBUG_MIDICC')
    if conf.options.debug_mts:
        conf.env.mydefines.append('DEBUG_MTS')
    if conf.options.debug_rpn:
        conf.env.mydefines.append('DEBUG_RPN')
    # waf doesn't seem to propagate configure-time options to sub-builds, so
    # we add the needed options to the environment instead.
    conf.env.dyn_manifest = conf.options.dyn_manifest
    conf.env.no_faust_install = conf.options.no_faust_install
    conf.env.no_plugin_install = conf.options.no_plugin_install
    conf.env.plugindir = conf.options.plugindir
    conf.env.split_bundles = conf.options.split_bundles
    conf.env.lv2_install_path = (None if conf.env.no_plugin_install
                                 else '%s/%s' % (conf.options.lv2dir,
                                                 conf.env.plugindir))
    conf.env.faust_install_path = (None if conf.env.no_faust_install
                                   else conf.options.faustdir)

from os.path import splitext

def build(bld):
    if bld.env.faust_install_path:
        bld.install_files(bld.env.faust_install_path,
                          ['lv2.cpp', 'lv2synth.cpp',
                           'lv2-manifest-template.ttl',
                           'lv2-manifest-static-template.ttl'])
    bld.recurse(subdirs)
    if not bld.env.split_bundles:
        # concatenate all the individual plugin manifests to one big manifest
        sources = bld.path.ant_glob('*/*.dsp')
        manifests = ['%s-manifest.ttl' % splitext(x.relpath())[0]
                     for x in sources]
        bld(rule = 'cat ${SRC} > ${TGT}',
            source = manifests,
            target = 'lv2/%s/manifest.ttl' % bld.env.plugindir,
            name = 'manifest',
            install_path = bld.env.lv2_install_path)

def dist(dst):
    dst.excl = ' build **/.hg* **/.waf-1* **/*~ **/*.pyc **/*.swp **/.lock-w*'
